import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wu_you/data/repository/trad_repository.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/utils/utils.dart';
import 'package:wu_you/utils/utils_image.dart';
import 'package:wu_you/widgets/text_form_field_widget.dart';

///买家申诉
class AppealOrderPage extends StatefulWidget{
  final String id;
  AppealOrderPage({Key key,this.id}):super(key: key);
  @override
  AppealOrderPageState createState() => AppealOrderPageState();
}
class AppealOrderPageState extends State<AppealOrderPage>{
  final _controller = TextEditingController();
  final _picker = ImagePicker();
  String netUrl;

  var message;
  @override
  Widget build(BuildContext context) {
    message = AppLocalizations.of(context);
    Widget child;
    if(netUrl == null){
      child = Image.asset(Utils.getImgPath("ic_complaints_add"),width: 100,height: 100,);
    }else{
      child = Image.network(netUrl,width: 100,height: 100,fit: BoxFit.fill,);
    }
    return Scaffold(
      appBar: AppBar(
        title: Text("${message['trad97']}"),
      ),
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("${message['trad102']}:"),
              Container(
                height: 90,
                margin: EdgeInsets.only(top: 12),
                padding: EdgeInsets.all(12),
                decoration: new BoxDecoration(
                    borderRadius: new BorderRadius.circular((5.0)),
                    border: Border.all(color: HexColor("#F6F6F6"),width: 1)
                ),
                child: TextFormFieldWidget(
                  hintText: "${message['trad103']}",
                  maxLines: 10,
                  needUnderline: false,
                  maxLength: 100,
                  controller: _controller,
                ),
              ),
              SizedBox(height: 18,),
              Text("${message['trad121']}:"),
              SizedBox(height: 12,),
              GestureDetector(
                child: child,
                onTap: () async{
                  closeKeyboard(context);
                  PickedFile value = await _picker.getImage(source: ImageSource.gallery, imageQuality: 100,);
                  File file = File(value.path);
                  File newFile = await ImageUtils.imageCompressAndGetFile(file);
                  LoadingApi.func(TradRepository().uploadImage(newFile,3), context, complete: (v) {
                    if (v == null) {
                      return;
                    }
                    setState(() {
                      netUrl = v;
                    });
                  });
                },
              ),

              Container(
                height: 40,
                margin: EdgeInsets.only(left: 12,right: 12,top: 30),
                child: ElevatedButton(
                  child: Text("${message['trad104']}"),
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(AppColors.primaryColor),
                    shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))),//圆角弧度
                  ),
                  onPressed: (){
                    if(_controller.text == ""){
                      // ToastUtils.show("请输入内容", context);
                      return;
                    }
                    LoadingApi.func(TradRepository().orderAppeal(widget.id, _controller.text), context,complete: (v){
                      Navigator.of(context).pop(1);
                      ToastUtils.show("${message['Success']}", context);
                    },error: (e){
                      ToastUtils.show("$e", context);
                    });
                  },
                ),
              )
            ],
          ),
        )
      ),
    );
  }

}